package IO;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import DataModel.*;
import MySqlAccess.RatingInfo;
import MySqlAccess.UserRating;
import Recommender.Covisited.MaTrixCovisited;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;


public final class ReadFile {

    public static int fold;
    public ReadFile() {
    }

    private class ComperatorDataMode implements Comparator<DataModel> {

        @Override
        public int compare(DataModel d1, DataModel d2) {
            return d1.GetIDUser() > d2.GetIDUser() ? 1 : ((d1.GetIDUser() == d2.GetIDUser()) ? 0 : -1);
        }
    }
   
    public ArrDataModel ReadTrainFile(String path) throws IOException {

        UserRating userRating = new UserRating();
        FileWriter outfile = new FileWriter("Train.txt");
        List<RatingInfo> lstRatingInfo = userRating.GetAllItem();
        List<DataModel> lstDataModel = new ArrayList<DataModel>();
        for (int i = 0; i < lstRatingInfo.size(); i++) {
            if (i % 9 != 0 && i % 10 != 0) {
                int time = lstRatingInfo.get(i).m_dateTime.getMonth() * 30 * 24 * 60 * 60 + lstRatingInfo.get(i).m_dateTime.getDate() * 24 * 60 * 60 + lstRatingInfo.get(i).m_dateTime.getHours() * 60 * 60 + lstRatingInfo.get(i).m_dateTime.getMinutes() * 60 + lstRatingInfo.get(i).m_dateTime.getSeconds();
                
                    outfile.write("" + i);
                    outfile.write("\r\n");
                    lstDataModel.add(new DataModel(lstRatingInfo.get(i).m_idUser, lstRatingInfo.get(i).m_idItem, lstRatingInfo.get(i).m_rating, time));
                    
            }

        }
        outfile.close();




        Collections.sort(lstDataModel, new ComperatorDataMode());
        int size = lstDataModel.size();
        DataModel[] newDataModel = new DataModel[lstDataModel.size()];
        for (int i = 0; i < size; i++) {
            newDataModel[i] = lstDataModel.get(i);
        }
        int numofrow = lstDataModel.size();
        ArrDataModel arrDataModel = new ArrDataModel(numofrow, newDataModel);
        return arrDataModel;


    }

    public ArrDataModel TestFileRandom() throws IOException {
        ArrDataModel arr = new ArrDataModel();
        ArrDataModel arrtrain = ReadTrainFile("pp");
        int count = 0;
        DataModel[] model = new DataModel[arrtrain.numofrow];
        for (int i = 0; i < arrtrain.numofrow && count < 2000; i++) {
            int[] Item = arrtrain.UserRating(arrtrain.arrmodel.get(i).GetIDUser());
            if ((i % 9 == 0 || i % 10 == 0 || i % 8 == 0) && Item.length > 5) {
                model[count] = new DataModel(arrtrain.arrmodel.get(i).iIDUser,
                        arrtrain.arrmodel.get(i).iIDItem, arrtrain.arrmodel.get(i).iRating,
                        arrtrain.arrmodel.get(i).iTimeStamp);

                count++;

            }

        }

        DataModel[] model2 = new DataModel[count];
        for (int i = 0; i < count; i++) {
            model2[i] = new DataModel(model[i].iIDUser, model[i].iIDItem, model[i].iRating, model[i].iTimeStamp);
        }
        arr.numofrow = count;
        arr = new ArrDataModel(count, model2);
        return arr;

    }

    public ArrDataModel TestFile20() throws IOException {
        ArrDataModel arr = new ArrDataModel();
        ArrDataModel arrtrain = ReadTrainFile("pp");
        int count = 0;
        DataModel[] model = new DataModel[arrtrain.numofrow];
        for (int i = 0; i < arrtrain.numofrow && count < 2000; i++) {
            int[] Item = arrtrain.UserRating(arrtrain.arrmodel.get(i).GetIDUser());
            if (Item.length > 20) {
                model[count] = new DataModel(arrtrain.arrmodel.get(i).iIDUser,
                        arrtrain.arrmodel.get(i).iIDItem,
                        arrtrain.arrmodel.get(i).iRating, arrtrain.arrmodel.get(i).iTimeStamp);
                count++;
            }

        }

        DataModel[] model2 = new DataModel[count];
        for (int i = 0; i < count; i++) {
            model2[i] = new DataModel(model[i].iIDUser, model[i].iIDItem, model[i].iRating, model[i].iTimeStamp);

        }
        arr.numofrow = count;
        arr = new ArrDataModel(count, model2);
        return arr;

    }

    public ArrDataModel TestFile() throws IOException {
        ArrDataModel arr = new ArrDataModel();
        ArrDataModel arrtrain = ReadTrainFile("pp");
        int count = 0;
        DataModel[] model = new DataModel[arrtrain.numofrow];
        for (int i = 0; i < arrtrain.numofrow; i++) {
            int[] Item = arrtrain.UserRating(arrtrain.arrmodel.get(i).GetIDUser());
            if (Item.length > 50) {
                model[count] = new DataModel(arrtrain.arrmodel.get(i).iIDUser,
                        arrtrain.arrmodel.get(i).iIDItem,
                        arrtrain.arrmodel.get(i).iRating, arrtrain.arrmodel.get(i).iTimeStamp);
                count++;


            }

        }

        DataModel[] model2 = new DataModel[count];
        for (int i = 0; i < count; i++) {
            model2[i] = new DataModel(model[i].iIDUser, model[i].iIDItem, model[i].iRating, model[i].iTimeStamp);

        }
        arr.numofrow = count;
        arr = new ArrDataModel(count, model2);
        return arr;


    }
      public ArrDataModel TestFile5() throws IOException {
        ArrDataModel arr = new ArrDataModel();
        ArrDataModel arrtrain = ReadTrainFile("pp");
        int count = 0;
        DataModel[] model = new DataModel[arrtrain.numofrow];
        for (int i = 0; i < arrtrain.numofrow; i++) {
            int[] Item = arrtrain.UserRating(arrtrain.arrmodel.get(i).GetIDUser());
            if (Item.length <20) {
                model[count] = new DataModel(arrtrain.arrmodel.get(i).iIDUser,
                        arrtrain.arrmodel.get(i).iIDItem, arrtrain.arrmodel.get(i).iRating,
                        arrtrain.arrmodel.get(i).iTimeStamp);
                count++;


            }

        }

        DataModel[] model2 = new DataModel[count];
        for (int i = 0; i < count; i++) {
            model2[i] = new DataModel(model[i].iIDUser, model[i].iIDItem, model[i].iRating, model[i].iTimeStamp);

        }
        arr.numofrow = count;
        arr = new ArrDataModel(count, model2);
        return arr;


    }

    public ArrDataModel ReadTestFile(String path) {

        UserRating userRating = new UserRating();
        List<RatingInfo> lstRatingInfo = userRating.GetAllItem();
        List<DataModel> lstDataModel = new ArrayList<DataModel>();
        for (int i = 0; i < lstRatingInfo.size(); i++) {
            //  if (i % 9 == 0 || i % 10 == 0) {
            if (lstRatingInfo.get(i).m_idUser < 100) {
                int time = lstRatingInfo.get(i).m_dateTime.getMonth() * 30 * 24 * 60 * 60 + lstRatingInfo.get(i).m_dateTime.getDate() * 24 * 60 * 60 + lstRatingInfo.get(i).m_dateTime.getHours() * 60 * 60 + lstRatingInfo.get(i).m_dateTime.getMinutes() * 60 + lstRatingInfo.get(i).m_dateTime.getSeconds();
               
                    lstDataModel.add(new DataModel(lstRatingInfo.get(i).m_idUser, lstRatingInfo.get(i).m_idItem, lstRatingInfo.get(i).m_rating, time));
                
            }
        }
        Collections.sort(lstDataModel, new ComperatorDataMode());
        int size = lstDataModel.size();
        DataModel[] newDataModel = new DataModel[lstDataModel.size()];
        for (int i = 0; i < size; i++) {
            newDataModel[i] = lstDataModel.get(i);
        }
        int numofrow = lstDataModel.size();
        ArrDataModel arrDataModel = new ArrDataModel(numofrow, newDataModel);
        return arrDataModel;
    }

    public static void main(String[] args) throws IOException {

        ReadFile r= new ReadFile();
        r.ReadTrainFile("pp");
    }
}
